package com.afd.ozblip.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.afd.ozblip.activity.R;
import com.afd.ozblip.entity.UserEntity;
import com.afd.ozblip.helper.PeopleVH;
import com.afd.ozblip.image.LoadImage;
import com.afd.ozblip.util.CollectionUtil;
import com.afd.ozblip.util.Constants;
import com.afd.ozblip.util.StringUtil;

public class PeopleListAdapter extends BaseAdapter {

	public List<UserEntity> dataList;
	private LayoutInflater inflater;
	private Context context;
	private LoadImage loadImage;

	public PeopleListAdapter(Context paramContext) {
		context = paramContext;
		this.dataList = new ArrayList<UserEntity>();
		this.inflater = LayoutInflater.from(context);
		this.loadImage = new LoadImage(context);
	}

	/**
	 * 获取列表条数
	 */
	@Override
	public int getCount() {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.size();
		} else {
			return 0;
		}

	}

	@Override
	public Object getItem(int paramInt) {
		if (CollectionUtil.UnNullAndZero(this.dataList)) {
			return this.dataList.get(paramInt);
		} else {
			return null;
		}
	}

	@Override
	public long getItemId(int paramInt) {
		return paramInt;
	}

	@Override
	public View getView(int position, View paramView, ViewGroup paramViewGroup) {
		try {
			PeopleVH peopleVH = null;
			if (paramView == null) {
				peopleVH = new PeopleVH();
				paramView = this.inflater.inflate(R.layout.nearby_people_item,
						null);

				peopleVH.peopleNameTv = (TextView) paramView
						.findViewById(R.id.tv_nearby_people_nickName);
				peopleVH.peopleImageIv = (ImageView) paramView
						.findViewById(R.id.iv_nearby_people_headImg);
				peopleVH.peopleDistanceTv = (TextView) paramView
						.findViewById(R.id.tv_nearby_people_distance);
				paramView.setTag(peopleVH);
			} else {
				peopleVH = (PeopleVH) paramView.getTag();
			}

			UserEntity people = this.dataList.get(position);
			if (people != null) {
				// 昵称
				String nickName = people.getUserNickName();
				if (!StringUtil.isNullOrEmpty(nickName)) {
					peopleVH.peopleNameTv.setText(nickName);
				}
				// 用户头像
				String headUrl = people.getUserHeadUrl();
				if (!StringUtil.isNullOrEmpty(headUrl)) {
					loadImage.addTask(Constants.WEB_PATH + headUrl,
							peopleVH.peopleImageIv);
				} else {
					peopleVH.peopleImageIv
							.setImageResource(R.drawable.default_head_pic);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paramView;
	}

	/**
	 * 添加多项
	 */
	public void addItems(List<UserEntity> commentList) {
		this.dataList.addAll(commentList);
	}

	/**
	 * 清除所有项
	 */
	public void removeAllItem() {
		this.dataList.clear();
	}
}
